layui.use(['form', 'layer', 'laydate'], function () {
    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        optionGroup = 0,
        company_id = sessionStorage["company_id"];

    //监听题型单选按钮 按需渲染 选项 1:单选,2:多选,3:填空,4:判断,5:简答
    form.on('radio(questionType)', function (data) {
        switch (data.value) {
            case '1':
                $('#option1').show();
                $('#option2').hide();
                $('#option3').hide();
                optionGroup = 1;
                break;
            case '2':
                $('#option1').hide();
                $('#option2').show();
                $('#option3').hide();
                optionGroup = 2;
                break;
            case '4':
                $('#option1').hide();
                $('#option2').hide();
                $('#option3').show();
                optionGroup = 4;
                break;
            default:
                $('#option1').hide();
                $('#option2').hide();
                $('#option3').hide();
                optionGroup = 0;
                break;
        }
    })

    form.on("submit(editQuestionBase)", function (data) {
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候', { icon: 16, time: false, shade: 0.8 });
        const { questionBaseId: id, subject, answer, questionType, school, course } = data.field;
        var questionBase = {
            id,
            subject,
            answer,
            questionType,
            school,
            course,
            company_id
        }

        if (optionGroup == 0 || optionGroup == 1) {//默认单选
            questionBase.option = data.field.option1;
        } else if (optionGroup == 2) {//多选
            data.field.optionA = data.field.optionA ? data.field.optionA : '';
            data.field.optionB = data.field.optionB ? data.field.optionB : '';
            data.field.optionC = data.field.optionC ? data.field.optionC : '';
            data.field.optionD = data.field.optionD ? data.field.optionD : '';
            data.field.optionE = data.field.optionE ? data.field.optionE : '';
            data.field.optionF = data.field.optionF ? data.field.optionF : '';
            questionBase.option = data.field.optionA + data.field.optionB + data.field.optionC + data.field.optionD + data.field.optionE + data.field.optionF;
        } else if (optionGroup == 4) {//判断
            questionBase.option = data.field.option3;
        } else {
            questionBase.option = "";
        }
        if (data.field.addOrUpdate == 0) {//添加
            //查重
            $.ajax({
                url: baseUrl + "questionBase/getQuestionBaseBySubject.php",
                data: questionBase,
                type: "POST",
                dataType: "text",
                success: function (data) {
                    console.log(data);
                    if (data != '0') {
                        message(index, '该题已存在不能重复添加！')
                    } else {
                        $.ajax({
                            url: baseUrl + "questionBase/addQuestionBase.php",
                            data: questionBase,
                            type: "POST",
                            dataType: "text",
                            success: function (data) {
                                if (data == "1") {
                                    message(index, '添加题库信息成功！')
                                } else {
                                    message(index, '添加题库信息失败！')
                                }
                            }
                        })
                    }
                }
            })



        } else {//修改
            console.log(questionBase);
            $.ajax({
                url: baseUrl + "questionBase/updateQuestionBase.php",
                data: questionBase,
                type: "POST",
                dataType: "text",
                success: function (data) {
                    if (data == "1") {
                        message(index, '修改题库信息成功！')
                    } else {
                        message(index, '修改题库信息失败！')
                    }
                }
            })
        }
        return false;
    })

    function message(index, msg) {
        setTimeout(function () {
            top.layer.close(index);
            top.layer.msg(msg);
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        }, 1000);
    }
})